package com.vincent.springboot.webflux.handler;

import com.vincent.springboot.webflux.domain.User;
import com.vincent.springboot.webflux.repository.UserDao;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.LocalDateTime;

/**
 * @author vincent.li
 */
@Component
@RequiredArgsConstructor
public class UserHandler {

    private final UserDao userDao;

    public Mono<User> save(User user) {
        return userDao.save(user);
    }

    public Mono<User> findById(Long id) {
        return userDao.findById(id);
    }

    public Flux<User> findAll() {
        return userDao.findAll();
    }

    public Mono<User> modify(User user) {
        user.setUpdateTime(LocalDateTime.now());
        return userDao.save(user);
    }

    public Mono<Void> delete(Long id) {
        return  userDao.deleteById(id);
    }
}
